package guiLayer;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

import modelLayer.*;

import controlLayer.*;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

public class mainGUI extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultTableModel totalSwaggers;
	private DefaultTableModel topSwaggers;
	private DefaultTableModel searchedSwaggers;
	
	private JTable totalSwagTable;
	private JTable topSwagTable;
	private JTable searchedSwagTable;
	
	
	private JComboBox<String> searchCriteria;
	private JComboBox<String> topCrit;

	private JPanel contentPane;
	private JPanel charPanel;
	private JPanel topCharPanel;
	
	private ControlCalculations calcCtrl;
	private ControlPerson persCtrl;
	private SplashScreen splScr;
	
	private float totalYolo= 1, totalCool = 1, totalSwag = 1, totalLame = 1;
	private float topSwag1 = 1, topSwag2 = 1, topSwag3 = 1;
	
	private TableColumn id, fName, lName, age, yolo, cool, swag, lame;
	
	private DefaultTableColumnModel header;
	private DefaultTableColumnModel topHeader;
	
	private static final int W = 280;
	private static final int H = 300;
	private JTextField txtWriteNameHere;
	private JTextField txtWriteLastName;
	private JTextField txtWriteAgeHere;
	private JTextField textField;
	
	DefaultPieDataset totalResult;
	DefaultPieDataset topResult;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					final mainGUI frame = new mainGUI();
					
					try {
			            // Set System L&F
			        UIManager.setLookAndFeel(
			            UIManager.getSystemLookAndFeelClassName());
			    } 
			    catch (UnsupportedLookAndFeelException e) {
			       // handle exception
			    }
			    catch (ClassNotFoundException e) {
			       // handle exception
			    }
			    catch (InstantiationException e) {
			       // handle exception
			    }
			    catch (IllegalAccessException e) {
			       // handle exception
			    }
					
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public mainGUI() throws IOException {
		
		persCtrl = new ControlPerson();
		calcCtrl = new ControlCalculations();
		splScr = new SplashScreen();
		
		final Image image = ImageIO.read(new File("background.png"));
		ImagePanel imagePanel = new ImagePanel(image);
		final DefaultPieDataset totalResult = new DefaultPieDataset();
		totalResult.setValue("Total Swag", totalSwag);
		totalResult.setValue("Total YOLO", totalYolo);
		totalResult.setValue("Total Cool", totalCool);
		totalResult.setValue("Total Lame", totalLame);
		final DefaultPieDataset topResult = new DefaultPieDataset();
		topResult.setValue("1st", topSwag1);
		topResult.setValue("2nd", topSwag2);
		topResult.setValue("3rd", topSwag3);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1300, 600);
		setLocationRelativeTo(null);
		
		
		contentPane = new JPanel();
		setUndecorated(true);
		setResizable(false);
		
		
		 id = new TableColumn(0);
		 id.setHeaderValue("#");
		 id.setPreferredWidth(10);
		 id.setResizable(false);
		 
		 fName = new TableColumn(1);
		 fName.setHeaderValue("First Name");
		 fName.setPreferredWidth(40);
		 fName.setResizable(false);
		 
		 lName = new TableColumn(2);
		 lName.setHeaderValue("Last Name");
		 lName.setPreferredWidth(40);
		 lName.setResizable(false);
		 
		 age = new TableColumn(3);
		 age.setHeaderValue("Age");
		 age.setPreferredWidth(40);
		 age.setResizable(false);
		 
		 yolo = new TableColumn(3);
		 yolo.setHeaderValue("Yolo");
		 yolo.setPreferredWidth(40);
		 yolo.setResizable(false);
		 
		 cool = new TableColumn(4);
		 cool.setHeaderValue("Cool");
		 cool.setPreferredWidth(40);
		 cool.setResizable(false);
		 
		 swag = new TableColumn(5);
		 swag.setHeaderValue("Swag");
		 swag.setPreferredWidth(40);
		 swag.setResizable(false);
		 
		 lame = new TableColumn(6);
		 lame.setHeaderValue("Lame");
		 lame.setPreferredWidth(40);
		 lame.setResizable(false);
		 
		 header = new DefaultTableColumnModel();
		 header.addColumn(id);
		 header.addColumn(fName);
		 header.addColumn(lName);
		 header.addColumn(age);
		 
		 topHeader = new DefaultTableColumnModel();
		 topHeader.addColumn(id);
		 topHeader.addColumn(fName);
		 topHeader.addColumn(lName);
		 topHeader.addColumn(yolo);
		 topHeader.addColumn(cool);
		 topHeader.addColumn(swag);
		 topHeader.addColumn(lame);
		
		
		
		String allSwaggers [] = {"#","First Name", "LName", "Age", "Swag", "Yolo", "Cool", "Lame"};
		String topSwaggersTop[] = {"#", "Name", "Swag", "YOLO", "Cool", "Lame"};
		
		
		
		totalSwaggers = new DefaultTableModel(null, allSwaggers);
		topSwaggers = new DefaultTableModel(null, allSwaggers);
		searchedSwaggers = new DefaultTableModel(null, topSwaggersTop);
		
		
		
		topSwagTable = new JTable(topSwaggers);
		
		
		totalSwagTable = new JTable(totalSwaggers);

		searchedSwagTable = new JTable(searchedSwaggers);
		
		
		JScrollPane topSwags = new JScrollPane(topSwagTable);
		JScrollPane totalSwags = new JScrollPane(totalSwagTable);
		JScrollPane searchSwags = new JScrollPane(searchedSwagTable);
		searchSwags.setEnabled(false);
		
		updateRows(totalSwagTable);
		
		
		topSwagTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent e)
			{
				if ( e.getValueIsAdjusting()) return;
				int selectedRow = topSwagTable.convertRowIndexToModel(topSwagTable.getSelectedRow());
				
				Vector data = (Vector) topSwaggers.getDataVector().elementAt(selectedRow);
				System.out.println(data);
				System.out.println(data.get(1));
				String inputSwag = (String) data.get(4);
				String inputYolo = (String) data.get(5);
				String inputCool = (String) data.get(6);
				String inputLame = (String) data.get(7);
				
				float tempSwag = Float.valueOf(inputSwag);
				float tempYolo = Float.valueOf(inputYolo);
				float tempCool = Float.valueOf(inputCool);
				float tempLame = Float.valueOf(inputLame);
				
				totalResult.setValue("Total Swag", tempSwag);
				totalResult.setValue("Total YOLO", tempYolo);
				totalResult.setValue("Total Cool", tempCool);
				totalResult.setValue("Total Lame", tempLame);
				
			}
		}
				
				
				);
		
		
		totalSwagTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent e)
			{
				if ( e.getValueIsAdjusting()) return;
				int selectedRow = totalSwagTable.convertRowIndexToModel(totalSwagTable.getSelectedRow());
				
				Vector data = (Vector) totalSwaggers.getDataVector().elementAt(selectedRow);
				System.out.println(data);
				System.out.println(data.get(1));
				String inputSwag = (String) data.get(4);
				String inputYolo = (String) data.get(5);
				String inputCool = (String) data.get(6);
				String inputLame = (String) data.get(7);
				
				float tempSwag = Float.valueOf(inputSwag);
				float tempYolo = Float.valueOf(inputYolo);
				float tempCool = Float.valueOf(inputCool);
				float tempLame = Float.valueOf(inputLame);
				
				totalResult.setValue("Total Swag", tempSwag);
				totalResult.setValue("Total YOLO", tempYolo);
				totalResult.setValue("Total Cool", tempCool);
				totalResult.setValue("Total Lame", tempLame);
				
			}
		}
				
				
				);
		
		
		setContentPane(contentPane);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(imagePanel, GroupLayout.PREFERRED_SIZE, 1299, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(1, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(imagePanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);
		charPanel = new JPanel();
		
		ChartPanel chartPanel = new ChartPanel(chartMaker(totalResult, totalSwag, totalYolo, totalCool, totalLame ), W, H, W, H, W, H,
	            false, true, true, true, true, true);
		charPanel.add(chartPanel);
		
		chartPanel.setMaximumSize(new Dimension(W,H));
		
		charPanel.setLayout(new FlowLayout());
		charPanel.setPreferredSize(new Dimension(W,H));
		charPanel.setMaximumSize(new Dimension(charPanel.getPreferredSize()));
		
		
	
		
		topCharPanel = new JPanel();
		
		topCharPanel.setLayout(new FlowLayout());
		topCharPanel.setPreferredSize(new Dimension(W,H));
		topCharPanel.setMaximumSize(new Dimension(topCharPanel.getPreferredSize()));
		
	
		
		JLabel lblSearchSwaggers = new JLabel("Search Swaggers");
		lblSearchSwaggers.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
		
		JLabel lblNewSwagger = new JLabel("New Swagger");
		lblNewSwagger.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
		
		JLabel lblName = new JLabel("First Name");
		lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblAge = new JLabel("Age");
		lblAge.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		txtWriteNameHere = new JTextField();
		txtWriteNameHere.setColumns(10);
		
		txtWriteLastName = new JTextField();
		txtWriteLastName.setColumns(10);
		
		txtWriteAgeHere = new JTextField();
		txtWriteAgeHere.setColumns(10);
		
		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		
		
		searchCriteria = new JComboBox<String>();
		searchCriteria.addItem("First Name");
		searchCriteria.addItem("Last Name");
		searchCriteria.addItem("Age");
		
		
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JLabel lblAllSwaggers = new JLabel("All Swaggers");
		lblAllSwaggers.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
		
		JLabel lblTopSwaggers = new JLabel("Top Swaggers");
		lblTopSwaggers.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearTable(topSwagTable);
				searchRows(topSwagTable);
				
				
			}
		});
		
		

		topCrit = new JComboBox<String>();
		topCrit.addItem("Swag");
		topCrit.addItem("Cool");
		topCrit.addItem("YOLO");
		topCrit.addItem("Lame");
		
		JButton button = new JButton("Search");

		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearTable(totalSwagTable);
				updateRows2(totalSwagTable);
			}
		});
		
		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				insertSwagger();
				Component frame = null;
				JOptionPane.showMessageDialog(frame,
					    "You have created a new user");
			}
		});
		
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		GroupLayout gl_imagePanel = new GroupLayout(imagePanel);
		gl_imagePanel.setHorizontalGroup(
			gl_imagePanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_imagePanel.createSequentialGroup()
					.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_imagePanel.createSequentialGroup()
							.addComponent(charPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
								.addComponent(btnRefresh, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblAllSwaggers)
								.addGroup(gl_imagePanel.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(totalSwags, GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE))))
						.addGroup(gl_imagePanel.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_imagePanel.createSequentialGroup()
									.addComponent(topSwags, GroupLayout.PREFERRED_SIZE, 370, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING, false)
										.addComponent(btnSearch, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(textField)
										.addComponent(searchCriteria, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)))
								.addComponent(lblSearchSwaggers))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_imagePanel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_imagePanel.createSequentialGroup()
							.addComponent(separator, GroupLayout.PREFERRED_SIZE, 11, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_imagePanel.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_imagePanel.createSequentialGroup()
									.addGroup(gl_imagePanel.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(lblLastName, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblName, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblAge))
									.addGap(14))
								.addComponent(lblNewSwagger))
							.addGap(18)
							.addGroup(gl_imagePanel.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(txtWriteAgeHere, Alignment.LEADING)
								.addComponent(txtWriteLastName, Alignment.LEADING)
								.addComponent(txtWriteNameHere, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_imagePanel.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnCreate, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_imagePanel.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnExit, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)))
							.addGap(26))
						.addGroup(gl_imagePanel.createSequentialGroup()
							.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_imagePanel.createSequentialGroup()
									.addComponent(topCrit, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(button, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_imagePanel.createParallelGroup(Alignment.TRAILING)
									.addComponent(lblTopSwaggers)
									.addComponent(searchSwags, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addComponent(topCharPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		);
		gl_imagePanel.setVerticalGroup(
			gl_imagePanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_imagePanel.createSequentialGroup()
					.addContainerGap(11, Short.MAX_VALUE)
					.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
						.addComponent(topCharPanel, GroupLayout.PREFERRED_SIZE, 308, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
							.addGroup(Alignment.TRAILING, gl_imagePanel.createSequentialGroup()
								.addComponent(lblTopSwaggers, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(searchSwags, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_imagePanel.createParallelGroup(Alignment.BASELINE)
									.addComponent(topCrit, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
									.addComponent(button))
								.addGap(9)
								.addGroup(gl_imagePanel.createParallelGroup(Alignment.TRAILING, false)
									.addGroup(gl_imagePanel.createSequentialGroup()
										.addComponent(separator, GroupLayout.PREFERRED_SIZE, 329, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED))
									.addGroup(gl_imagePanel.createSequentialGroup()
										.addComponent(lblNewSwagger, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
										.addGap(23)
										.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
											.addGroup(gl_imagePanel.createSequentialGroup()
												.addComponent(txtWriteNameHere, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addComponent(txtWriteLastName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addComponent(txtWriteAgeHere, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addGroup(gl_imagePanel.createSequentialGroup()
												.addComponent(lblName)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addComponent(lblLastName)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addComponent(lblAge)))
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(btnCreate)
										.addGap(13)
										.addComponent(btnExit)
										.addGap(35))))
							.addGroup(gl_imagePanel.createSequentialGroup()
								.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
									.addComponent(charPanel, GroupLayout.PREFERRED_SIZE, 309, GroupLayout.PREFERRED_SIZE)
									.addGroup(gl_imagePanel.createSequentialGroup()
										.addComponent(lblAllSwaggers, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
										.addGap(7)
										.addComponent(totalSwags, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(btnRefresh)))
								.addGap(10)
								.addComponent(lblSearchSwaggers, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_imagePanel.createParallelGroup(Alignment.LEADING)
									.addComponent(topSwags, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
									.addGroup(gl_imagePanel.createSequentialGroup()
										.addComponent(searchCriteria, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(btnSearch))))))
					.addGap(11))
		);
		//ChartPanel topChartPanel = new ChartPanel(chartMaker2(topSwag1, topSwag2, topSwag3), W, H, W, H, W, H,
	     //       false, true, true, true, true, true);
		ChartPanel topChartPanel = new ChartPanel(chartMaker2(topResult, topSwag1, topSwag2, topSwag3), W, H, W, H, W, H,
	            false, true, true, true, true, true);
		topCharPanel.add(topChartPanel);
		topChartPanel.setMaximumSize(new Dimension(W,H));
		
		imagePanel.setLayout(gl_imagePanel);
		contentPane.setLayout(gl_contentPane);
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				clearTable(searchedSwagTable);
				searchTopSwags(searchedSwagTable);
				topResult.setValue("1st", topSwag1);
				topResult.setValue("2nd", topSwag2);
				topResult.setValue("3rd", topSwag3);
				
				
			}
		});
	}
	
	public JFreeChart chartMaker(DefaultPieDataset result, float totalSwag, float totalYolo, float totalCool, float totalLame)
	{
		
		result.setValue("Total Swag", totalSwag);
		result.setValue("Total YOLO", totalYolo);
		result.setValue("Total Cool", totalCool);
		result.setValue("Total Lame", totalLame);
		JFreeChart chart = ChartFactory.createPieChart("", result, false, false, false);
		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		
		return chart;
	}
	public JFreeChart chartMaker2(DefaultPieDataset result ,float totalSwag1, float topSwag2, float topSwag3)
	{
		

		JFreeChart chart = ChartFactory.createPieChart("", result, false, false, false);
		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		
		return chart;
	}
	
	public void updateRows(JTable table)
    {
		System.out.println("ui side");
		
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		
		ArrayList<Person> list = splScr.getAll();
		System.out.println("List Size:" + list.size());
		for(Person pers : list)
		{ 
			model.addRow(new String[]
					{
					String.valueOf(pers.getId()),
					String.valueOf(pers.getfName()),
					String.valueOf(pers.getlName()),
					String.valueOf(pers.getAge()),
					String.valueOf(pers.getTotalSwag()),
					String.valueOf(pers.getTotalYolo()),
					String.valueOf(pers.getTotalCool()),
					String.valueOf(pers.getTotalLame())
					});
			

		}
    }
	public void updateRows2(JTable table)
    {
		System.out.println("ui side");
		
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		
		ArrayList<Person> list = persCtrl.findAllPeople();
		System.out.println("List Size:" + list.size());
		for(Person pers : list)
		{ 
			model.addRow(new String[]
					{
					String.valueOf(pers.getId()),
					String.valueOf(pers.getfName()),
					String.valueOf(pers.getlName()),
					String.valueOf(pers.getAge()),
					String.valueOf(pers.getTotalSwag()),
					String.valueOf(pers.getTotalYolo()),
					String.valueOf(pers.getTotalCool()),
					String.valueOf(pers.getTotalLame())
					});
			

		}
    }
	
	public void topSwaggers(JTable table)
	{
		System.out.println("top swag");
	}
	
	public void clearTable(JTable table)
	{
		DefaultTableModel model =(DefaultTableModel) table.getModel();
		model.getDataVector().removeAllElements();
		model.fireTableDataChanged();		
	}
	
	public void searchTopSwags(JTable table)
	{
		System.out.println("search top");
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		ArrayList<Person> list = new ArrayList<Person>();
		System.out.println("List is : " + list);
		String param = topCrit.getSelectedItem().toString();
		if(param.equals("Swag"))
		{
			list = persCtrl.findBySwag();
			System.out.println("Swag");
			topSwag1 = list.get(0).getTotalSwag();
			topSwag2 = list.get(1).getTotalSwag();
			topSwag3 = list.get(2).getTotalSwag();
		}
		else if(param.equals("YOLO"))
		{
			list = persCtrl.findByYolo();
			topSwag1 = list.get(0).getTotalYolo();
			topSwag2 = list.get(1).getTotalYolo();
			topSwag2 = list.get(2).getTotalYolo();
		}
		else if(param.equals("Cool"))
		{
			list = persCtrl.findByCool();
			topSwag1 = list.get(0).getTotalCool();
			topSwag2 = list.get(1).getTotalCool();
			topSwag3 = list.get(2).getTotalCool();
		}
		else if(param.equals("Lame"))
		{
			list = persCtrl.findByLame();
			topSwag1 = list.get(0).getTotalLame();
			topSwag2 = list.get(1).getTotalLame();
			topSwag3 = list.get(2).getTotalLame();
		}
		
		for(Person pers : list)
		{
			model.addRow( new String[]
					{
					String.valueOf(pers.getId()),
					String.valueOf(pers.getfName()),
					String.valueOf(pers.getTotalSwag()),
					String.valueOf(pers.getTotalYolo()),
					String.valueOf(pers.getTotalCool()),
					String.valueOf(pers.getTotalLame())
					});
			
			
		}
		
		
		
	}
	
	public void searchRows(JTable table)
	{
		
		System.out.println("search swag");
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		ArrayList<Person> searchedList = new ArrayList<Person>();
		System.out.println("List is : " + searchedList);
		String check1 = "First Name";
		String check2 = "Last Name";
		String check3 = "Age";
		String parameter = searchCriteria.getSelectedItem().toString();
		if(parameter.equals(check1))
		{
			searchedList = persCtrl.findByFName(textField.getText());
			System.out.println("List is : " + searchedList);
		}
		else if(parameter.equals(check2))
		{
			searchedList = persCtrl.findByLName(textField.getText());
			System.out.println("List is : " + searchedList);
		}
		else if(parameter.equals(check3))
		{
			if(textField.getText().matches(".*\\d.*"))
			{
			searchedList = persCtrl.findByAge(Integer.parseInt(textField.getText()));
			System.out.println("List is : " + searchedList);
			}
			else
			{
				Component frame = null;
				JOptionPane.showMessageDialog(frame,
					    "You have not entered a proper age");
			}
		}
		
		
		for(Person pers : searchedList)
		{
			model.addRow( new String[]
					{
					String.valueOf(pers.getId()),
					String.valueOf(pers.getfName()),
					String.valueOf(pers.getlName()),
					String.valueOf(pers.getAge()),
					String.valueOf(pers.getTotalSwag()),
					String.valueOf(pers.getTotalYolo()),
					String.valueOf(pers.getTotalCool()),
					String.valueOf(pers.getTotalLame())
					});
			
			
		}
		
		
	}
	public void insertSwagger()
	{
		
		
		persCtrl.setfName(txtWriteNameHere.getText());
		persCtrl.setlName(txtWriteLastName.getText());
		persCtrl.setAge(Integer.parseInt(txtWriteAgeHere.getText()));
		System.out.println("The age of the swaggers is : " + txtWriteAgeHere.getText());
		String str = txtWriteNameHere.getText() + txtWriteLastName.getText();
		calcCtrl.setSizeOfArray(str.length());
		System.out.println("Length of array is : " + str.length());
		System.out.println(txtWriteNameHere.getText() + " " + txtWriteLastName.getText());
		calcCtrl.putStringinArray(txtWriteNameHere.getText(),txtWriteLastName.getText());
		//calcCtrl.setArraySize();
		System.out.println("Size of array is : "  + calcCtrl.getArraySize());
		int tempor = Integer.parseInt(txtWriteAgeHere.getText());
		calcCtrl.setSwag(tempor);
		System.out.println("The current swag is " + calcCtrl.getSwag());
		calcCtrl.setYolo(Integer.parseInt(txtWriteAgeHere.getText()));
		calcCtrl.setCool(Integer.parseInt(txtWriteAgeHere.getText()));
		calcCtrl.setLame(Integer.parseInt(txtWriteAgeHere.getText()));
		
		
		float swag = calcCtrl.getSwag();
		float yolo = calcCtrl.getYolo();
		float cool = calcCtrl.getCool();
		float lame = calcCtrl.getLame();
		System.out.println(
				txtWriteNameHere.getText()+ " " +
				txtWriteLastName.getText()+ " " +
				txtWriteAgeHere.getText() + " " +
				
				str +  " " +
				swag+  " " +yolo+  " " +cool+  " " +lame
				
				);
		persCtrl.insertNewPerson(txtWriteNameHere.getText(), txtWriteLastName.getText(), Integer.parseInt(txtWriteAgeHere.getText()), swag, yolo, cool, lame);
		
		
	}
}

